class AIArcher extends AI{
	
	//check if players are too close to archer
	private var _retreatRange:int;
	private var _isRetreat:boolean;
			
	public function AIArcher(selectedEnemy:EnemyCharacter, playerList:Array){
		super(selectedEnemy, playerList);
		
		checkDistanceRange();
		
		_retreatRange = 3;
		_isRetreat = false;
	}
	
	protected function checkDistanceRange(){
		
		for (var i = 0; i < _playerList .length; i++)
		{
			if(_playerList[i].isVisibleToEnemy() && !_playerList[i].getIsDead()){
				var dstCoord :Vector2 = _playerList[i].getCoord();
				var distance = Vector2.Distance(_srcCoord, dstCoord);
	
				if(distance < _closestDistance){
					_closestDistance = distance;
					_closestPlayer = _playerList[i];
					//Debug.Log("Closest Player: "+_closestPlayer);
				}
				//Debug.Log("Closest Distance: "+_closestDistance+" | Attack Range: "+_atkRange);
				if (distance <= _atkRange)
				{
					_isInRange = true;	
					_playersInRange.Push(_playerList[i]);
					
					// target players not in cover first
					if(!_playerList[i].isInCover() && distance <= _closestDistance){	
						_closestPlayer = _playerList[i];
					}				
					//Debug.Log("AI in range: "+_playerList [i]+" | "+distance);					
				}
				
				if (distance <= _retreatRange)
				{
					_isRetreat = true;
				}
				
				if(distance <= _detectFlankRadius && _previousFlankable.length == 0){
					_flankablePlayers.Push(_playerList[i]);
				}
			}
		}				
		
		if(_previousFlankable.length == 0){
			_selectedEnemy.setPreviousFlankable(_flankablePlayers);
		}else{
			_flankablePlayers = _previousFlankable;
		}
		
		if(_closestPlayer != null){
		
			//raycast to the list of players for visibility check
			var raySource:Vector3 = _selectedEnemy.getSurfaceCoord();				
			var rayDst:Vector3 = _closestPlayer.getSurfaceCoord();
			var hit:RaycastHit;	
			//convert player to obstacle layer so that raycast will detect obstacles
			_closestPlayer.gameObject.layer = LayerMask.NameToLayer("Obstacle");
			//raycast only to the player layer
			if (Physics.Raycast(raySource, rayDst - raySource, hit, _atkRange,1<<LayerMask.NameToLayer("Obstacle"))){
				//casted enemies, setSelectable(true)
				if(!hit.collider.gameObject.tag.Equals("PlayerCharacter")){
					_isInRange = false;
				}
			}
			_closestPlayer.gameObject.layer = LayerMask.NameToLayer("Player");
			
			
			_coord = _closestPlayer.getCoord();
		
		}
	}	
	
	//Getters
	public function getIsRetreat():boolean { return _isRetreat; }
	
	public function getCoverTile(mapReference:Map){
		return getCoverTile(_retreatRange, mapReference);
	}
	
	public function getForwardCoverTile(mapReference:Map){
		return getForwardCoverTile(_retreatRange, mapReference);
	}
	
	function getHighestHitPlayer():GameCharacter{
		var highestHitPlayer:GameCharacter = null;
		var highestHitChance = 0;
		
		for(var i:int=0; i<_playersInRange.length; i++){
			var currentHitChance = _playersInRange[i].calculateHitChance(_selectedEnemy);
			
			if(currentHitChance > highestHitChance){
				highestHitChance = currentHitChance;
				highestHitPlayer = _playersInRange[i];
			}
		}
		
		return highestHitPlayer;
	}
}